package org.jeecg.modules.demo.temple.pay.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.JxyhPayHttpRequestUtil;
import org.jeecg.common.util.JxyhPaySignatureUtil;
import org.jeecg.modules.demo.temple.order.entity.TempleActivityOrder;
import org.jeecg.modules.demo.temple.order.mapper.TempleActivityOrderMapper;
import org.jeecg.modules.demo.temple.pay.service.IJxyhPayService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class JxyhPayServiceImpl implements IJxyhPayService {
    @Override
    public Map<String, String> payWechatOrder(String transAmt,
                                        String orderTitle,
                                        String orderDesc,
                                        String orderNo,
                                        String merchantNo,
                                        String notifyUrl,
                                        String returnUrl,
                                        String priKey,
                                        String pubKey,
                                        String apiKey,
                                              String appId,
                                              String openId)  throws Exception{
        Map<String, String> map = new HashMap<String,String>();
        double d = Double.valueOf(transAmt);
        int i = (int)d;//去除多余的小数位，100.0=》100
        map.put("amt_trans", String.valueOf(i));//交易金额
        map.put("busino", merchantNo);//商户号
        map.put("param_type","0");
        map.put("sub_appid",appId);
        map.put("sub_openid",openId);
        map.put("channel_type", "08");//渠道类型:07-PC 08-WAP，固定传08
        map.put("confflag", "9");//确认收货标志, 0线上确认收货，8线上待收货(暂不启用)，9线下免确认收货。默认请填9
        map.put("currency", "156");//货币代码，默认156
        map.put("encode", "UTF-8");//编码方式
        map.put("notify_url", notifyUrl);//响应url
        map.put("orderdesc", orderDesc);//商品描述
        map.put("orderno", orderNo);//商户订单号，商户端保证唯一
        map.put("ordertitle", orderTitle);//订单主题
        map.put("qrcode_type", "02");//00-聚合支付，01-支付宝，02-微信，03-银联 不送则默认聚合
        map.put("return_url", returnUrl);//前台返回商户结果时使用
        map.put("serialno", orderNo);//交易流水号
        map.put("subcode", "guokai.gpay.applyQrcode");//写死
        map.put("onlineflag","1");
        SimpleDateFormat dateFormat = new SimpleDateFormat("YYYYMMDD");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HHmmss");
        Date curDate = new Date();
        map.put("trdate",  DateUtils.date2Str(curDate,dateFormat));//交易日期
        map.put("trtime",  DateUtils.date2Str(curDate,timeFormat));//交易时间
        map.put("trtype", "GP00011");//交易代码 /IMP-CPos/b2c/pay/busApplyQrcode.do
        map.put("version", "1.0.0");//版本号,固定填写
        map.put("sign_type", "RSA");//签名方式，固定填写
        JxyhPaySignatureUtil jxyhPaySignatureUtil = new JxyhPaySignatureUtil();
        String signMsg = jxyhPaySignatureUtil.rsa256Sign(map,apiKey,priKey);
        if (!signMsg.isEmpty()) {
            map.put("sign", signMsg);
            Map<String, String> resultMap = new HashMap<>();
                // 调用接口的方法：参数一：请求的接口地址，参数二：入参的map
                String result = JxyhPayHttpRequestUtil.sendPost("https://netbank.jx-bank.com/pos/IMP-CPos/b2c/pay/busConsumeByParam.do", JxyhPayHttpRequestUtil.getParamStr(map));
                resultMap = JxyhPayHttpRequestUtil.getParamMap(result, false, "UTF-8");
                // 验签方法：参数一：入参map，参数二：jxbank_pubkey.cert，参数三：商户的安全校验码，此方法用于商户侧验签；银行侧的验签方法是把参数2改为busi_pubkey_xxx.cert
                if (jxyhPaySignatureUtil.verify256Sign(resultMap, pubKey, apiKey)) {
                    return resultMap;
                } else {
                    throw new RuntimeException("江西银行支付下单返回报文验签失败");
                }

        } else {
            throw new RuntimeException("江西银行支付下单签名失败");
        }

    }

    @Override
    public Map<String, String> queryOrder(Map<String, String> params) {
        return null;
    }

    @Override
    public Map<String, String> closeOrder(Map<String, String> params) {
        return null;
    }

    public static void main(String[] args) {
        String a = "110.0";
        double d = Double.valueOf(a);
        int i = (int)d;
        System.out.println(String.valueOf(i));
    }
}
